package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.UserService;
import java.util.List;
import java.util.Map;

import jnr.ffi.annotations.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author liangyuanshao
 * @date 2021/11/19 - 23:21
 */
@RestController
public class UserController {
  @Autowired
  UserService userService;

  @PostMapping("/queryUserList")
  public Result getUsers(){
    Result result=new Result();
    result.setSuccess(false);

    result.setDetail(userService.list());
    result.setSuccess(true);
    result.setMsg("所有用户获取成功");
    return result;
  }


//  //添加某个员工,成功返回1，不成功返回0
//  @RequestMapping("/addUser")
//  public boolean addUser(@PathVariable("id") Integer id){
//    return false;
//  }

  // 通过id删除员工,成功返回1，不成功返回0
  @PostMapping("/deleteUser")
  public Result delete(@RequestParam("id") Integer id){
    Result result=new Result();
    result.setSuccess(false);
    result.setDetail(userService.getById(id));
    userService.removeById(id);
    result.setSuccess(true);
    result.setMsg("删除用户成功");
    return result;
  }

  @RequestMapping("/get_user_info")
  public Result getUserInfo(HttpSession session){
    Result result = new Result();
    result.setSuccess(false);
    result.setMsg(null);
    Integer userId =(Integer) session.getAttribute("id");
    User user = null;
    QueryWrapper<User> wrapper = new QueryWrapper<User>();
    wrapper.eq("userId",userId);
    try {
      user=userService.getOne(wrapper);
      result.setSuccess(true);
    }catch (Exception exception){
      exception.printStackTrace();
    }
    result.setMsg("获取成功");
    result.setDetail(user);
    return result;
  }
}
